package com.hson.demo.base.util;

import java.io.File;
import java.io.InputStream;
import java.util.Properties;

/**
 * File Utilities
 * 
 * @author Simon.Ye <samlinye@163.com>
 */
public final class FileUtil {
	/**
	 * 构造函数私有化
	 */
	private FileUtil() {}
	
	/**
	 * 取得文件的扩展名，如：jpg
	 * 
	 * @author Simon.Ye <samlinye@163.com>
	 * 
	 * @param fileName 文件名
	 * @return
	 */
	public static String getExtension(String fileName) {
		if (fileName == null || "".equals(fileName) || !fileName.contains(".")) {
			return "";
		}
		
		return fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
	}
	
	/**
	 * 取得文件的扩展名，如：jpg
	 * 
	 * @author Simon.Ye <samlinye@163.com>
	 * 
	 * @param file java.io.File 对象
	 * @return
	 */
	public static String getExtension(File file) {
		try {
			if (file == null || !file.isFile()) {
				return "";
			}
			
			return getExtension(file.getName());
		} catch (Exception e) {
			e.printStackTrace();
			
			return "";
		}
	}

	
	/**
	 * 读取 properties 文件
	 * 
	 * @param resourceFileName 资源文件名
	 * @return
	 */
	public static Properties loadPropertiesFromResourceFile(String resourceFileName) {
		Properties properties = new Properties();
		
		InputStream in = null;
		
		try {
			in = FileUtil.class.getClassLoader().getResourceAsStream(resourceFileName);
			
			properties.load(in);
			
			return properties;
		} catch (Exception e) {
			return properties;
		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	}
}